from utils.log_util import logger
from utils.mysql_util import db

#获取短信验证码
def get_code(mobile):
    #sql = 'select code from users_verifycode where mobile = '%s' order by id desc limit 1;' %mobile
    sql = 'select code from users_verifycode where mobile = {} order by id desc limit 1;'.format(mobile)
    result = db.select_db_one(sql)
    logger.info('sql执行结果：{}'.format(result))#括号里的内容同等于f'sql执行结果：{result}'
    return result['code']
#删除测试的用户数据
def delete_user(mobile):
    sql = 'delete from users_userprofile where mobile = {};'.format(mobile)
    result = db.execute_db(sql)
    logger.info('sql执行结果：{}'.format(result))
#根据手机号删除短信验证码，从而避免触发一分钟内重复发送短信验证码的限制
def delete_code(mobile):
    sql = 'delete from users_verifycode where mobile = {};'.format(mobile)
    result = db.execute_db(sql)
    logger.info('sql执行结果：{}'.format(result))
#查询userid
def user_id(mobile):
    sql = 'select id from users_userprofile where mobile = {};'.format(mobile)
    result = db.select_db_one(sql)
    return result['id']
#查询购物车商品数量
def get_shop_cart_num(username, good_id):
    #查询UID
    id = user_id(username)
    sql = "select nums from trade_shoppingcart where user_id = %d and goods_id = %d;" % (id, good_id)
    result = db.select_db_one(sql)
    return result['nums']

